package com.gsafety.library.common;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;

import com.gsafety.library.Base.IBaseDialog;
import com.gsafety.library.Exts.Ext;
import com.gsafety.library.R;

/**
 * 系统弹窗
 */
public class SystemAlertDialogI extends IBaseDialog
{
    private TextView title_tv;
    private TextView message_tv;
    private TextView left_btn;
    private TextView right_btn;
    private TextView bottom_btn;

    private String title = "";
    private String message = "";
    private String completeText = "";
    private String navigateText = "";
    private SystemAlertCallBack navigateBlock = null;
    private SystemAlertCallBack completeBlock = null;


    public SystemAlertDialogI(
            Context context,
            String title,
            String message,
            String completeText,
            String navigateText,
            boolean cancel
    )
    {
        super(context, R.layout.alert_system_layout);

        this.title = title == null ? context.getString(R.string.dialog_title_text) : title;
        this.message = message == null ? "" : message;
        this.completeText = completeText == null ? context.getString(R.string.dialog_confirm_text) : completeText;
        this.navigateText = navigateText == null ? "" : navigateText;

        setWidth(0.69f);
        setCancelable(cancel);
    }

    @Override
    public void initView(AlertDialog alertDialog, View view)
    {
        title_tv = view.findViewById(R.id.title);
        message_tv = view.findViewById(R.id.message);
        left_btn = view.findViewById(R.id.left_btn);
        right_btn = view.findViewById(R.id.right_btn);
        bottom_btn = view.findViewById(R.id.bottom_btn);

        title_tv.setText(title);
        if (message.isEmpty())
        {
            Ext.gone(message_tv);
        } else
        {
            message_tv.setText(message);
        }

        if (navigateText.isEmpty())
        {
            Ext.gone(left_btn);
            Ext.gone(right_btn);
            bottom_btn.setText(completeText);
        } else
        {
            Ext.gone(bottom_btn);
            right_btn.setText(completeText);
            left_btn.setText(navigateText);
        }

    }

    @Override
    public void initEvent(AlertDialog alertDialog, View view)
    {
        right_btn.setOnClickListener(new OnUnshakClickListener()
        {
            @Override
            public void onClick(View view, long lastClickTime)
            {
                try
                {
                    dismiss();
                    if (completeBlock != null)
                    {
                        completeBlock.call();
                    }
                } catch (Exception e)
                {
                    e.printStackTrace();
                }
            }
        });
        left_btn.setOnClickListener(new OnUnshakClickListener()
        {
            @Override
            public void onClick(View view, long lastClickTime)
            {
                try
                {
                    dismiss();
                    if (navigateBlock != null)
                    {
                        navigateBlock.call();
                    }
                } catch (Exception e)
                {
                    e.printStackTrace();
                }
            }
        });
        bottom_btn.setOnClickListener(new OnUnshakClickListener()
        {
            @Override
            public void onClick(View view, long lastClickTime)
            {
                try
                {
                    dismiss();
                    if (completeBlock != null)
                    {
                        completeBlock.call();
                    }
                } catch (Exception e)
                {
                    e.printStackTrace();
                }
            }
        });
    }

    public void setNavigateBlock(SystemAlertCallBack navigateBlock)
    {
        this.navigateBlock = navigateBlock;
    }

    public void setCompleteBlock(SystemAlertCallBack completeBlock)
    {
        this.completeBlock = completeBlock;
    }
}
